package com.hankol.community

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.extension.checkNotNull
import com.baselibrary.manager.LoadingManager
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleUtils
import com.baselibrary.utils.StatusBarUtil
import com.baselibrary.activity.BaseActivity
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.community.adapter.SquareSpecialColumnAdapter
import com.hankol.community.bean.SquareSpecialColumnBean
import com.hankol.community.databinding.ActivityColumListBinding
import com.hankol.community.view.GridSpaceItemDecoration
import com.hankol.community.viewmodel.CommunityViewModel


/**
 * @description 专栏列表【首页大改版】
 *@author wxp
 */
@Route(path = ActivityRouter.COMMUNITY.COLUMN_LIST)
class SpecialColumnListActivity : BaseActivity() {

    var page = 1
    private var hasnext: Boolean = false
    private val adapter by lazy { SquareSpecialColumnAdapter() }
    private lateinit var binding: ActivityColumListBinding
    private lateinit var mViewModel: CommunityViewModel

    override fun getLayoutId(): Int {
        return R.layout.activity_colum_list
    }

    override fun init() {
        setStatusBarHeight(R.color.color_F9F9F9)
        StatusBarUtil.setStatusBarDarkTheme(this, true)
        binding = getDataBinding<ActivityColumListBinding>()
        mViewModel = getActivityViewModelProvider(this).get(CommunityViewModel::class.java)

        LoadingManager.showLoadingDialog(this)
        initView()
        initListener()
        initObserver()
        loadData()
        SensorsDataManager.instance.articleView()
    }

    private fun initView() {
        binding.rvList.layoutManager = GridLayoutManager(this, 2)
        binding.rvList.addItemDecoration(GridSpaceItemDecoration(2, 20, 0))
        binding.rvList.adapter = adapter
    }

    private fun initObserver() {
        mViewModel.columnListMld.observe(this, Observer {
            LoadingManager.hideLoadingDialog(this)
            binding.smartRefresh.finishRefresh()
            if (it.data.size == 0) {
                //无数据
                if (page != 1) {
                    binding.smartRefresh.finishLoadMore()
                }
                adapter.emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_GOOD_LIST, binding.rvList, "暂无专栏")
            } else {
                if (it.data.checkNotNull()) {
                    hasnext = it.hasnext
                    binding.smartRefresh.finishLoadMore()
                    if (page == 1) {
                        adapter.setNewData(it.data)
                    } else {
                        adapter.addData(it.data)
                    }
                    page++
                } else {
                    binding.smartRefresh.finishLoadMore()
                }
            }

        })

    }

    private fun initListener() {
        binding.ivBack.setOnClickListener { finish() }
        adapter.setOnItemClickListener { adapter, view, position ->
            if (!DoubleUtils.isFastDoubleClick()) {
                //专栏详情
                val squareForumBean = adapter.data[position] as SquareSpecialColumnBean
                val map = HashMap<String, Any>()
                map["id"] = squareForumBean.id
                ARouterUtils.jump(ActivityRouter.COMMUNITY.COLUMN_DETAIL, map)
            }
        }

        binding.smartRefresh.setOnRefreshListener {
            page = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
    }

    fun loadData() {
        mViewModel.reqColumnListData(page)
    }
}